// components/manual/manual.js
import {throttle} from "../../utils/util"
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';
Component({
  /**
   * 组件的属性列表
   */
  properties: [
    "categoryFirst"
  ],
  /**
   * 组件的初始数据
   */
  data: {
    dataList:[],
    shopData:[],
    swiperList:[],
    // isClose:true,
    shopInfo:{
      categoryFirst:"",
      categorySecond:"",
      page:1,
      size:5
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
   async getSwiper(e){
    // console.log(e)
    let res = await getApp().api.getSwiperlist({categoryId:e})
    // console.log(res)
    if(res.code!=200)return wx.showToast({
      title: res.msg,
      icon:'error'
    })
    this.setData({
      swiperList:res.data,
    })
    },
   async getover(e){
    // console.log(e)
    this.setData({
      ['shopInfo.categoryFirst']:e,
      ['shopInfo.page']:1,
      ['shopInfo.categorySecond']:"",
      dataList:[],
      shopData:[]
    })
    //获取二级菜单
    let res = await getApp().api.findCategory({categoryFirst:e})
    if(res.code!=200)return
      this.setData({
       dataList:res.data
     })
     //获取商品
     this.getShops()
     this.getSwiper(e)
  },
  //点击跳转商品详情
  siwperTap(e){
    // console.log(e.currentTarget.id)
    wx.navigateTo({
      url: '/packageA/pages/shopDetail/index?id='+e.currentTarget.id,
    })
  },
  //二级菜单点击
  handleChildData(e){
      // console.log('二级菜单点击')
      if(e.detail){
        this.setData({
          ['shopInfo.categorySecond']:this.data.dataList[e.detail].id,
          ['shopInfo.page']:1
        })
      }else{
        this.setData({
          ['shopInfo.categorySecond']:"",
          ['shopInfo.page']:1
        })
      }
      this.getShops()
  },
  async getShops(){
    // console.log(this.data.shopInfo)
    let res = await getApp().api.getShoplist(this.data.shopInfo)
    // console.log(res)
     if(res.code!=200)return
     if(JSON.stringify(res.data)=='{}'&&this.data.shopInfo.page!=1)return wx.showToast({
       title: '暂无更多商品',
       icon:'none'
     })
     if(JSON.stringify(res.data)=='{}'&&this.data.shopInfo.page==1){
      wx.showToast({
        title: '暂无商品',
        icon:'none'
      })
       return this.setData({
          shopData:[]
       })
     }
     res.data.data.forEach(element => {
      element.picUrls= JSON.parse(element.picUrls.replace(/\'/g,"\""))
      element.yyTimes?JSON.parse(element.yyTimes):[]
     })
     if(this.data.shopInfo.page==1){
      this.setData({
        shopData:res.data.data
      })
     }else{
     
      this.setData({
        shopData:this.data.shopData.concat(res.data.data)
      })
      if(res.data.data.length<=0){
        this.setData({
          ['shopInfo.page']:this.data.shopInfo.page-1
        })
      }
     }
    },
 //添加购物车
  async collectTo(e){
    // console.log(e)
    let i =  e.currentTarget.id
   if(this.data.shopData[i].shoppingCart){//取消购物车
        let result = await getApp().api.clearCollect([this.data.shopData[i].id])
        // console.log(result)
        if(result.code!=200)return wx.showToast({
          title: result.msg,
          icon: 'error',
          duration: 2000
        })
        wx.showToast({
          title: '购物车中已取消',
          icon: 'none',
          duration: 2000
        })
          this.setData({
            [`shopData[${i}].shoppingCart`]:false,
          })   
   }else{ //添加
    let data = {
      spuId:this.data.shopData[i].id,
      addPrice:this.data.shopData[i].paymentPrice,
      quantity:1,
      spuName:this.data.shopData[i].name,
      size:''
     }
    // console.log(data)
     let res= await getApp().api.postCollect(data)
      // console.log(res)
     if(res.code!=200)return Toast.fail(res.msg);
    this.setData({
      [`shopData[${i}].shoppingCart`]:true,
    })
    //修改状态
    wx.showToast({
      title: '添加成功',
      icon: 'none',
      duration: 2000
    })
   } 

},
//shopsTo收藏
async shopsTo(e){
  let i =  e.currentTarget.id
   // console.log(this.data.shopData[i])
   let data =  {
    collector:"1",
    spuCode:this.data.shopData[i].id
   }
   let res = await getApp().api.postCarts(data)
   // console.log(res)
   if(res.code!=200)return
   if(res.msg=="取消收藏成功"){
    this.setData({
      [`shopData[${i}].collect`]:false
    })
   }else if(res.msg=="收藏成功"){
    this.setData({
      [`shopData[${i}].collect`]:true
    })
   }  
   wx.showToast({
    title: res.msg,
    icon: 'none',
    duration: 2000
  })
},
   //商品滑动到底部
 tolower(e){throttle(()=>{
       this.setData({
        ['shopInfo.page']:this.data.shopInfo.page+1
       })
       this.getShops()
    })},
 
  },

})

